//
//  UILabel+Tools.m
//  Pods-YCBaseModule_Example
//
//  Created by Apple on 2022/11/2.
//

#import "UILabel+Tools.h"
#import "YCUtilityHeader.h"

@implementation UILabel (Tools)

+ (instancetype)labelWithText:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                         font:(UIFont *)font {
    return [self labelWithFrame:CGRectZero text:text textColor:textColor font:font textAlignment:NSTextAlignmentLeft numOfLines:1 bgColor:UIColor.clearColor];
}

+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font {
    return [self labelWithFrame:frame text:text textColor:textColor font:font textAlignment:NSTextAlignmentLeft numOfLines:1 bgColor:UIColor.clearColor];
}

+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                 textAlignment:(NSTextAlignment)alignment
{
    return [self labelWithFrame:frame text:text textColor:textColor font:font textAlignment:alignment numOfLines:1 bgColor:UIColor.clearColor];
}

+ (instancetype)labelWithText:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                 textAlignment:(NSTextAlignment)alignment
{
    return [self labelWithFrame:CGRectZero text:text textColor:textColor font:font textAlignment:alignment numOfLines:1 bgColor:UIColor.clearColor];
}

+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                 textAlignment:(NSTextAlignment)alignment
                       bgColor:(UIColor *)bgColor {
    return [self labelWithFrame:frame text:text textColor:textColor font:font textAlignment:alignment numOfLines:1 bgColor:bgColor];
}

+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                 textAlignment:(NSTextAlignment)alignment
                    numOfLines:(NSInteger)numOfLines {
    return [self labelWithFrame:frame text:text textColor:textColor font:font textAlignment:alignment numOfLines:numOfLines bgColor:UIColor.clearColor];
}

+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                 textAlignment:(NSTextAlignment)alignment
                    numOfLines:(NSInteger)numOfLines
                       bgColor:(UIColor *)bgColor {
    return [self labelWithFrame:frame text:text textColor:textColor font:font textAlignment:alignment numOfLines:numOfLines bgColor:bgColor cornerRadius:0];
}

+ (instancetype)labelWithFrame:(CGRect)frame
                          text:(nullable NSString *)text
                     textColor:(UIColor *)textColor
                          font:(UIFont *)font
                 textAlignment:(NSTextAlignment)alignment
                    numOfLines:(NSInteger)numOfLines
                       bgColor:(UIColor *)bgColor
                  cornerRadius:(CGFloat)cornerRadius {
    UILabel *lab = [[UILabel alloc] initWithFrame:frame];
    lab.text = text;
    lab.font = font;
    lab.textColor = textColor;
    lab.textAlignment = alignment;
    lab.numberOfLines = numOfLines;
    lab.backgroundColor = bgColor;
    lab.layer.cornerRadius = cornerRadius;
    lab.layer.masksToBounds = YES;
    return lab;
}


/// 两端对齐 注意要在给Lable的frame，text设置完之后再使用
- (void)textAlignmentLeftAndRight
{
    [self textAlignmentLeftAndRightWith:CGRectGetWidth(self.frame)];
}

/// 根据宽度直接对齐
/// @param labelWidth labelWidth description
- (void)textAlignmentLeftAndRightWith:(CGFloat)labelWidth
{
    if(self.text==nil||self.text.length == 0) {
        return;
    }
    CGSize size = [self.text boundingRectWithSize:CGSizeMake(labelWidth,MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:self.font} context:nil].size;
    NSInteger length = (self.text.length-1);
    NSString* lastStr = [self.text substringWithRange:NSMakeRange(self.text.length-1,1)];
    if([lastStr isEqualToString:@":"]||[lastStr isEqualToString:@"："]) {
        length = (self.text.length-2);
    }
    
    CGFloat margin = (labelWidth - size.width)/length;
    NSNumber *number = [NSNumber numberWithFloat:margin];
    NSMutableAttributedString* attribute = [[NSMutableAttributedString alloc]initWithString:self.text];
    [attribute addAttribute:NSKernAttributeName value:number range:NSMakeRange(0,length)];
    self.attributedText= attribute;
}

+ (void)changeLineSpaceForLabel:(UILabel *)label WithSpace:(float)space {
    NSString *labelText = label.text;
    if (kIsEmptyString(labelText)) {
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    label.attributedText = attributedString;
    [label sizeToFit];
}

- (void)changeLineSpaceForLabel:(float)space {
    NSString *labelText = self.text;
    if (kIsEmptyString(labelText)) {
        return;
    }
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:space];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
    self.attributedText = attributedString;
    [self sizeToFit];
}

@end
